package org.itboys.mobile.service.mongo.mobile;

import org.itboys.mobile.entity.lasted.SystemDictionary;
import org.itboys.mobile.service.lasted.SystemMobileDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.PearlAccount;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/10/12.
 * @author 朱坤涛
 */
@Service
public class PearlAccountService extends BaseService<PearlAccount,Long>{
    @Autowired
    private SystemMobileDictionaryService dicService;
    @Resource(name="pDS")
    private MongoDataSource pearlDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return pearlDataSource;
    }

    //新增和修改，新增保证是默认账户
    public void saveAccouunt(Long accountId,Long memberId,String name,String bank,String account){
        List<SystemDictionary> dictionaries = dicService.findByField("name",bank);
        String picture = ListUtil.isNotNull(dictionaries)?dictionaries.get(0).getPicture():null;

        if (accountId==null||accountId==0L){
            //新增账户为默认账户，找到以前的账户，全部设置为不默认账户
            List<PearlAccount> oldAccounts = super.findByField("memberId",memberId);
            for (PearlAccount oldAccount:oldAccounts){
                oldAccount.setIsDefault(SundayCommonConstants.NO);
            }
            super.batchUpdate(oldAccounts);
            //保存本次账户
            super.save(new PearlAccount(memberId,bank,name,account,SundayCommonConstants.YES,picture));
        }else {
            super.updateExceptEmpty(accountId,new PearlAccount(bank,name,account,picture));
        }
    }

    //获取默认地址
    public PearlAccount getDefaultAccount(Long memberId) {
        Map<String, Object> param = new HashMap<>();
        param.put("isDefault", SundayCommonConstants.YES);
        param.put("memberId", memberId);
        List<PearlAccount> accounts = super.list(param);

        if (accounts != null && accounts.size() > 0) {
            return accounts.get(0);
        }
        return null;
    }

    //设置默认地址
    public void setDefaultAccount(Long accountId,Long memberId){
        List<PearlAccount> accounts = super.findByField("memberId",memberId);
        for (PearlAccount account:accounts){
            if (account.getId()==accountId){
                account.setIsDefault(SundayCommonConstants.YES);
            }else {
                account.setIsDefault(SundayCommonConstants.NO);
            }
        }
        super.batchUpdate(accounts);
    }
    //删除地址
    public void deleteAccount(Long accountId){
        PearlAccount add = super.getById(accountId);
        if (add.getIsDefault()==SundayCommonConstants.YES){
            throw new ErrorException("mor地址不能删除");
        }
        super.update(accountId,"isDeleted",SundayCommonConstants.YES);
    }
}
